/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_FlightProcedure.h
 *
 *  Created on: 2022年12月30日
 *      Author: 64435
 */

#ifndef APP_LAYER_INC_LOS_FLIGHTPROCEDURE_H_
#define APP_LAYER_INC_LOS_FLIGHTPROCEDURE_H_

#include "rs.h"

#define CSP_CRC32_POLY 0x82F63B78L

//飞行任务链表项
typedef struct FlightTask
{
    uint32_t tasknum;     //任务号
    uint32_t extime;      //执行时间起点
    uint8_t* arg;         //指向参数的指针，需要动态分配，以字节为单位
    struct FlightTask* prev;   //上一个链表项
    struct FlightTask* next;   //下一个链表项
}FLIGHTTASK_t;

typedef struct RSEnDeCode
{
    uint8_t* origindata;  //原始数据
    uint8_t* encodedata;  //编码后的数据，会比原始数据长NROOTS(32)
    uint8_t* decodedata;  //解码后的数据，长度跟原始数据一样
    uint8_t length;       //原始数据的长度，不能长于 NN-NROOTS(255 - 32 = 223)
}RSEnDeCode_t;

extern FLIGHTTASK_t* phead;

uint8_t LOS_FP_Procedure(uint32_t sec);
void LOS_FP_ExtraTK(uint32_t sec);
FLIGHTTASK_t* LOS_FP_InitTable();
FLIGHTTASK_t* LOS_FP_CreateTable(uint32_t tasknum, uint32_t extime);
FLIGHTTASK_t* LOS_FP_ApplyNode(uint32_t num, uint32_t time);
FLIGHTTASK_t* LOS_FP_PushBack(uint32_t num, uint32_t time);
void LOS_FP_PushFront(uint32_t num, uint32_t time);
void LOS_FP_DeleteNode(FLIGHTTASK_t* pos);
uint32_t LOS_FP_GetTableSize();
void LOS_FP_CheckTable(uint32_t time);
FLIGHTTASK_t* LOS_FP_FindTimeUp(uint32_t time);
FLIGHTTASK_t* LOS_FP_SearchTask(uint32_t num);
void LOS_FP_FindFirstDelayCMD();
void LOS_FP_DBGPRT();
void LOS_FP_SafeMode();
void LOS_FP_ExitSafeMode();
uint32_t LOS_FP_CRC32Cal(const uint8_t * data, uint32_t length, uint32_t crc);
uint8_t LOS_FP_CRC8Cal(uint8_t* buf, uint32_t length);
uint16_t LOS_FP_CRC16Cal(uint8_t* data_buf, uint8_t start_byte, uint8_t length);
size_t LOS_FP_CobsEncode(const void *data, size_t length, uint8_t *buffer);
size_t LOS_FP_CobsDecode(const uint8_t *buffer, size_t length, void *data);
void LOS_FP_BLEndianTransfer(uint8_t* input, uint8_t length);
void LOS_FP_EnterNewModeSwitch(uint16_t mode);


#endif /* APP_LAYER_INC_LOS_FLIGHTPROCEDURE_H_ */
